﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Sistema
{
    public partial class FrmCadOS : Form
    {
        public FrmCadOS()
        {
            InitializeComponent();
            Fill_Combo();
        }

        void Fill_Combo()
        {

            Conexao.Conn.Open();
            string sql = "select nome from mydb.cliente";
            MySqlCommand cmd = new MySqlCommand(sql, Conexao.Conn);
            MySqlDataReader rs;


            try
            {


                rs = cmd.ExecuteReader();

                while (rs.Read())
                {
                    string name = rs.GetString("nome");
                    comboBox1.Items.Add(name);
                }
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.Message);

            }
            Conexao.Conn.Close();


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

            Conexao.Conn.Open();
            MySqlCommand sql = new MySqlCommand("insert into mydb.ordem_servico(codigo_cliente,descricao,valor,conta,data_emissao,empenho,baixa,contrato,ccm,banco,forma_pag,data_venc,retirado_por,rg_cpf_retirada) values(" + this.textBox3.Text + ",'" + this.textBox21.Text + "','"+this.textBox19.Text+"','" + this.textBox5.Text + "','" + this.dateTimePicker2.Text + "','" + this.textBox6.Text + "','" + this.textBox7.Text + "','" + this.textBox8.Text + "','" + this.textBox12.Text + "','" + this.textBox17.Text + "','" + this.textBox20.Text + "','" + this.dateTimePicker1.Text + "','" + this.textBox22.Text + "','" + this.textBox23.Text + "')", Conexao.Conn);
            sql.ExecuteNonQuery();
            Conexao.Conn.Close();

            MessageBox.Show("Ordem se Serviço cadastrada com sucesso! ");

            }
            catch(Exception ex)
            {
            MessageBox.Show(ex.Message);
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new FrmConsAgen().ShowDialog();
        }

        

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Conexao.Conn.Open();
            string sql = "select * from mydb.cliente where nome='" + comboBox1.Text + "'";
            MySqlCommand cmd = new MySqlCommand(sql, Conexao.Conn);
            MySqlDataReader rs;


            try
            {


                rs = cmd.ExecuteReader();

                while (rs.Read())
                {
                    string codigo = rs.GetInt32("codigo_cl").ToString();
                    textBox3.Text = codigo;
                    string cpf = rs.GetString("CPF/CNPJ");
                    textBox10.Text = cpf;
                    string rg = rs.GetString("rg");
                    textBox11.Text = rg;
                    string end = rs.GetString("endereco");
                    textBox1.Text = end;
                    string cid = rs.GetString("cidade");
                    textBox2.Text = cid;
                    string estado = rs.GetString("estado");
                    textBox13.Text = estado;
                    string cep = rs.GetString("cep");
                    textBox14.Text = cep;
                    string tel = rs.GetString("telefone");
                    textBox15.Text = tel;
                    string email = rs.GetString("email");
                    textBox16.Text = email;

                }

            }
            catch (MySqlException ee)
            {
                MessageBox.Show(ee.Message);

            }
            Conexao.Conn.Close();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            new FrmConsOS().ShowDialog();
        }

    }
}
